package cn.windy.samary.module.shiro.controller;

import cn.windy.samary.module.shiro.UserTypeToken;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author zyl
 * @date 2018/5/12 17:26
 */
@Controller
@RequestMapping
public class FrontController {

    @RequestMapping("/front/login")
    public String loginTo(HttpServletRequest request, HttpServletResponse response) {

        String username = request.getParameter("username");
        String password = request.getParameter("password");
        if (StringUtils.isEmpty(username) || StringUtils.isEmpty(password)) {
            return "/front/login";
        }
        Subject subject = SecurityUtils.getSubject();
        Md5Hash md5Hash = new Md5Hash(password, username, 10);
        UserTypeToken token = new UserTypeToken(username, md5Hash.toString(), "1");
        try {
            subject.login(token);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return "redirect:/front/authc/index";
    }



    @RequestMapping("/front/authc/index")
    public String index(HttpServletRequest request, HttpServletResponse response) {

        return "/front/index";
    }

    @RequestMapping("/front/authc/logout")
    public String logout(HttpServletRequest request, HttpServletResponse response) {
        Subject subject = SecurityUtils.getSubject();
        subject.logout();

        return "/front/index";
    }

    @RequestMapping("/front/authc/test")
    public String handlerTest(HttpServletRequest request, HttpServletResponse response) {

        return "/front/test";
    }

}
